/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.nonmember.forms;

import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import service.UserLoginServiceLocal;

/**
 *
 * @author SONY
 */
public class RegisterStrutsActionForm extends org.apache.struts.action.ActionForm {
    
    @EJB UserLoginServiceLocal userLoginServiceLocal;
    
    private String firstName;
    private String lastName;
    private String street;
    private String ward;
    private String district;
    private String city;
    private String province;
    private String phone;
    private String gender;
    private String email;
    private String password;
    private String confirmpass;


    /**
     *
     */
    public RegisterStrutsActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        
        String emailExpr = "(\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,6})";
        ServletContext context = servlet.getServletContext();
        userLoginServiceLocal = (UserLoginServiceLocal) context.getAttribute("userLoginServiceLocal");
        if (email.length() == 0) {
            errors.add("email", new ActionMessage("error.nonmember.register.emailField.required"));
        } else if(!email.matches(emailExpr)) {
           errors.add("email", new ActionMessage("error.nonmember.register.emailField.format"));
        } else if(userLoginServiceLocal.checkEmail(email)) {
            errors.add("email", new ActionMessage("error.nonmember.register.emailField.exist"));
        }
        if (password.length() == 0) {
            errors.add("password", new ActionMessage("error.nonmember.register.passwordField.required"));
        } else if(password.length() < 6 || password.length() > 20) {
           errors.add("password", new ActionMessage("error.nonmember.register.passwordField.range"));
        }
        if (confirmpass.length() == 0) {
            errors.add("confirmpass", new ActionMessage("error.nonmember.register.confirmpassField.required"));
        } else if (!confirmpass.equals(password)) {
            errors.add("confirmpass", new ActionMessage("error.nonmember.register.confirmpassField.compare"));
        }
        if (lastName.length() == 0) {
            errors.add("lastName", new ActionMessage("error.nonmember.register.lastNameField.required"));
        }
        if (firstName.length() == 0) {
            errors.add("firstName", new ActionMessage("error.nonmember.register.firstNameField.required"));
        }
        String phoneExpr = "([0-9]+)";
        if (phone.length() == 0) {
            errors.add("phone", new ActionMessage("error.nonmember.register.phoneField.required"));
        } else if(!phone.matches(phoneExpr)) {
           errors.add("phone", new ActionMessage("error.nonmember.register.phoneField.format"));
        }  
        if (street.length() == 0) {
            errors.add("street", new ActionMessage("error.nonmember.register.streetField.required"));
        }
        if (ward.length() == 0) {
            errors.add("ward", new ActionMessage("error.nonmember.register.wardField.required"));
        }
        if (district.length() == 0) {
            errors.add("district", new ActionMessage("error.nonmember.register.districtField.required"));
        }
        if (city.length() == 0) {
            errors.add("city", new ActionMessage("error.nonmember.register.cityField.required"));
        }                   
        
        return errors;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the street
     */
    public String getStreet() {
        return street;
    }

    /**
     * @param street the street to set
     */
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * @return the ward
     */
    public String getWard() {
        return ward;
    }

    /**
     * @param ward the ward to set
     */
    public void setWard(String ward) {
        this.ward = ward;
    }

    /**
     * @return the district
     */
    public String getDistrict() {
        return district;
    }

    /**
     * @param district the district to set
     */
    public void setDistrict(String district) {
        this.district = district;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the province
     */
    public String getProvince() {
        return province;
    }

    /**
     * @param province the province to set
     */
    public void setProvince(String province) {
        this.province = province;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the confirmpass
     */
    public String getConfirmpass() {
        return confirmpass;
    }

    /**
     * @param confirmpass the cPassword to set
     */
    public void setConfirmpass(String confirmpass) {
        this.confirmpass = confirmpass;
    }
}
